import os
import configparser
import time
import hashlib

ilevel ={"CRITICAL":50, "ERROR":40, "WARNING":30, "INFO":20, "DEBUG":10, "NOTSET":0}
path_file = "config.ini"
curpath = os.path.dirname(os.path.realpath(__file__))
cfgpath = os.path.join(curpath, path_file)
config = configparser.ConfigParser()
config.read(cfgpath)
level1 = config['log']['level'].upper()
loglevel = ilevel[level1]
logfile = config['log']['file']
AccessKey = config['obs']['AccessKey']
SecretKey = config['obs']['SecretKey']
obsServer = config['obs']['server']
bucketName = config['obs']['bucketname']
host = str(config['mysql']['host'])
port = int(config['mysql']['port'])
user = str(config['mysql']['user'])
password = str(config['mysql']['password'])
re_denoise = int(config['recover']['denoise'])
re_callback = int(config['recover']['denoise'])

hostname = config['host']['name']
process1 = int(config['host']['process1'])
process2 = int(config['host']['process2'])
process3 = int(config['host']['process3'])

if hostname == 'no':
    hn = str(int(time.time()))
    md= hashlib.md5()# 创建md5对象
    md.update(hn.encode(encoding='utf-8'))
    md5str = md.hexdigest()
    hostname = md5str
    config.set('host', 'name', md5str)        #设置为新值
    with open(cfgpath, 'w') as fw:   #循环写入
        config.write(fw)